// TypeScript file
module Animation {
    enum Animation { "bolt", "Boom", "Gold", "Shield", "fly" }
    /**
     * 动画基础类--郭启航
     */
    export class BasicAnimaton {

        /**初始化动画*/
        public init(showPlane, AnimationManager, type) {
            this.AnimationPanle = showPlane;
            this.AnimationManager = AnimationManager;
            this.AnimationType = type;
        }
        /**动画*/
        public Animation: egret.MovieClip;
        /**动画的类型*/
        public AnimationType: number;
        /**动画Id*/
        public AnimationId: number;
        /**显示容器*/
        public AnimationPanle: egret.DisplayObjectContainer;
        /**动画管理器*/
        public AnimationManager: AnimationManager;
        /**是否显示在舞台上*/
        public AnimationIsLiving: boolean = false;
        /**动画Josn文件*/
        public AnimationJosn: string;
        /**动画图集*/
        public AnimationAtlas: string;
        /**项目名*/
        public AnimationName: string;
        /**开始帧*/
        public AnimationStart: string;
        /**循环次数*/
        public AnimationCycleIndex: number = 1;
        /**判断是否播放完*/
        public AnimationIsPlay: boolean;

        /**读取动画配置*/
        public getConfig()
        { }
        /**创建动画*/
        public createAnimation(point: egret.Point, Type: number, id): egret.MovieClip {
            this.AnimationType = Type;
            this.AnimationId = id;
            var AnimationManager = Animation[this.AnimationType];
           // console.log(AnimationManager);
            this.AnimationIsLiving = false;
            this.AnimationIsPlay = false;
            this.AnimationJosn = AnimationManager + "_json";
            this.AnimationAtlas = AnimationManager + "_png";
            this.AnimationName = AnimationManager;
            this.AnimationStart = AnimationManager;
            if (this.AnimationType == 2 || this.AnimationType == 3 || this.AnimationType == 4) {
                this.AnimationCycleIndex = -1
            }
            else {
                this.AnimationCycleIndex = 1
            }
            var texture2mc = RES.getRes(this.AnimationAtlas);
            var data2mc = RES.getRes(this.AnimationJosn);
            let mcFactory: egret.MovieClipDataFactory = new egret.MovieClipDataFactory(data2mc, texture2mc);
            this.Animation = new egret.MovieClip(mcFactory.generateMovieClipData(this.AnimationName));
            this.Animation.gotoAndPlay(this.AnimationStart, this.AnimationCycleIndex);//"A" : Boom -1 ： 循环播放				
            this.Animation.x = point.x - 70;//帧动画位置
            this.Animation.y = point.y - 50;
            this.AnimationPanle.addChild(this.Animation);
            return this.Animation;
        }
        /**动画摧毁*/
        public Animationdestory(Animation: egret.MovieClip, Type) {
            if (Type == 2) {
                this.AnimationManager.destoryOneAnmition(this.AnimationType)
                var gold = egret.Tween.get(Animation).to({ x: 50, y: 50 }, 500).call(() => {
                    if (this.AnimationPanle) {
                        this.AnimationPanle.removeChild(Animation)
                    }
                });
            } else {
                this.AnimationManager.destoryOneAnmition(this.AnimationType)
                Animation.addEventListener(egret.Event.COMPLETE, () => {
                    if (this.AnimationPanle) {
                        this.AnimationPanle.removeChild(Animation)
                    }
                }, this);
            }
            // if (Type == 4) {
            //     this.AnimationPanle.removeChild(Animation)
            // }
        }
    }
}